package com.example.administrator.mvptest.view.view;
 //http://blog.csdn.net/qq_16628781/article/details/51455320
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.BinderThread;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;

import com.example.administrator.mvptest.R;
import com.example.administrator.mvptest.view.adapter.ZhihuStoryAdapter;
import com.example.administrator.mvptest.view.databeans.ZhihuStory;
import com.example.administrator.mvptest.view.presenter.MainPresenter;
import com.example.administrator.mvptest.view.view.IViewBind.IMainActivity;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements IMainActivity {

ZhihuStoryAdapter mAdapter;
    @InjectView(R.id.zhihudaily_list)
    RecyclerView mZhihudailyList;
    @InjectView(R.id.activity_main)
    RelativeLayout mActivityMain;

   // @InjectView(R.id.)
    private MainPresenter mainPresenter=new MainPresenter(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        mZhihudailyList.setLayoutManager(new LinearLayoutManager(this));
        loadData();
    }
//    @OnClick({R.id.tonews,R.id.https_friendly,R.id.https_unfriendly})
//    public  void onClick(View view)
//    {
//        switch (view.getId())
//        {
//            case R.id.tonews:
//                Intent intent=new Intent(this,Nes)
//        }
//    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void loadData() {
        mainPresenter.loadData();
    }

    @Override
    public void getDataSuccess(ArrayList<ZhihuStory> stories) {
  //不管界面怎么改 只要与presenter进行绑定都得到的是stories的数据，view界面只负责展示
        if(mainPresenter!=null)
        {
          mAdapter.addItem(stories);
        }
        else{
            mAdapter=new ZhihuStoryAdapter(this,stories);
            mZhihudailyList.setAdapter(mAdapter);
        }
    }

    @Override
    public void getDataFail(String errCode, String errMsg) {
        Snackbar.make(mActivityMain,errMsg,Snackbar.LENGTH_SHORT).show();
    }
}
